package com.kx.push.service.impl;

import com.kx.push.mapper.IdemMapper;
import com.kx.push.mapper.MessageMapper;
import com.kx.push.pojo.Idem;
import com.kx.push.pojo.IdemExample;
import com.kx.push.pojo.Message;
import com.kx.push.service.PushService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Sun
 * @Date 2021/7/26 17:24
 */
@Service
public class PushServiceImpl implements PushService {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IdemMapper idemMapper;

    @Override
    public boolean isIdem(Long requestId) {
        IdemExample example = new IdemExample();
        IdemExample.Criteria criteria = example.createCriteria();
        criteria.andRequestIdEqualTo(requestId);
        List<Idem> idems = idemMapper.selectByExample(example);
        //判断数据库中是否存在requestId
        if (idems != null && idems.size() > 0) {
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)   //出现什么异常时候进行回滚
    @Override
    public void saveMessage(Message msg, Long requestId) {
        Idem idem = new Idem();
        idem.setRequestId(requestId);
        idem.setTime(System.currentTimeMillis());
        idemMapper.insertSelective(idem);
        messageMapper.insertSelective(msg);
    }
}
